package com.zpark.cz.simple;


import com.rabbitmq.client.*;
import com.zpark.cz.util.ConnectionUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    private static final String QUEUE_NAME = "A1.queue";
    public static void main(String[] args) throws IOException, TimeoutException {
        // 获取连接
        Connection conn = ConnectionUtil.getConnection();
        // 从连接中获取通道
        Channel channel = conn.createChannel();
        // 声明队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        // 定义队列的消费者
        DefaultConsumer cousumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                // 获取到消息字节流，并封装成String
                String msg = new String(body, "utf-8");
                System.out.println("消费者，收到消息msg -> " + msg);
            }
        };
        // 监听队列
        channel.basicConsume(QUEUE_NAME, false, cousumer);
    }

}
